package com.sais.manage.boot.platform.service.impl;


import com.sais.manage.boot.platform.dto.PersonalDTO;
import com.sais.manage.boot.platform.dto.UserInfoDTO;
import com.sais.manage.boot.platform.entity.UserInfo;
import com.sais.manage.boot.platform.mapper.UserInfoMapper;
import com.sais.manage.boot.platform.mapper.UserMapper;
import com.sais.manage.boot.platform.service.IPersonalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Slf4j
@Service
public class PersonalServiceImpl implements IPersonalService {

    @Resource
    UserMapper userMapper;

    @Resource
    UserInfoMapper userInfoMapper;

    @Override
    public boolean updatePersonal(PersonalDTO personalDTO) {
        UserInfo userInfo = personalDTO.convert();

        UserInfoDTO info = userInfoMapper.selectUserInfo(userInfo.getUserId());
        if (StringUtils.isEmpty(info)) {
            userInfoMapper.saveUserInfo(userInfo);
        } else {
            userInfo.setGmtModified(LocalDateTime.now());
            userInfoMapper.updateById(userInfo);
        }

        return true;
    }

    @Override
    public UserInfoDTO selectUserInfo(String userId) {
        UserInfoDTO userInfo = userInfoMapper.selectUserInfo(userId);
        return userInfo;
    }
}
